package com.zkingsoft.externalInterface.business;

import org.apache.log4j.Logger;

import com.zkingsoft.externalInterface.common.Util;
import com.zkingsoft.externalInterface.protocol.payComProtocol.JsApiPayComReqData;
import com.zkingsoft.externalInterface.protocol.payComProtocol.JsApiPayComResData;
import com.zkingsoft.externalInterface.protocol.payProtocol.JsApiPayReqData;
import com.zkingsoft.externalInterface.protocol.payProtocol.JsApiPayResData;
import com.zkingsoft.externalInterface.protocol.queryProtocol.ScanPayQueryReqData;
import com.zkingsoft.externalInterface.protocol.queryProtocol.ScanPayQueryResData;
import com.zkingsoft.externalInterface.protocol.refundProtocol.RefundReqData;
import com.zkingsoft.externalInterface.protocol.refundProtocol.RefundResData;
import com.zkingsoft.externalInterface.service.JsApiPayComService;
import com.zkingsoft.externalInterface.service.JsApiPayService;
import com.zkingsoft.externalInterface.service.RefundService;
import com.zkingsoft.externalInterface.service.ScanPayQueryService;

/**
 * User: rizenguo Date: 2014/12/1 Time: 17:05
 */
public class JsApiPayBusiness {

	private JsApiPayService jsApiPayService;

	
    //打log用
    Logger log=Logger.getLogger(JsApiPayBusiness.class);
    
	public JsApiPayBusiness() throws Exception {
		jsApiPayService = new JsApiPayService();
	}
	
	

	/**
	 * 
	 * @Description:生成预付订单
	 * @author:姜友瑶
	 * @param scanPayReqData
	 *            返回类型 void
	 * @throws Exception
	 * @date 2016年10月13日
	 */
	public JsApiPayResData createPrapareOrder(JsApiPayReqData jsApiPayReqData) throws Exception {
		// 接受API返回
		String payServiceResponseString;
		
		long costTimeStart = System.currentTimeMillis();

		payServiceResponseString = jsApiPayService.request(jsApiPayReqData);
		
		long costTimeEnd = System.currentTimeMillis();
		long totalTimeCost = costTimeEnd - costTimeStart;
		
		log.info("------------------request createPrapareOrder taking ：" + totalTimeCost + "ms"+"--response="+payServiceResponseString);
		
		// 将从API返回的XML数据映射到Java对象
		JsApiPayResData jsApiPayResData = (JsApiPayResData) Util.getObjectFromXML(payServiceResponseString,
				JsApiPayResData.class);
		
		return jsApiPayResData;
	}
	
	/**
	 * 
	 * @Description:支付成功后查询微信订单
	 * @author:guchunxia
	 * @param scanPayReqData
	 *            返回类型 void
	 * @throws Exception
	 * @date 2016年10月13日
	 */
	public ScanPayQueryResData queryPrapareOrder(ScanPayQueryService scanPayQueryService ,ScanPayQueryReqData scanPayQueryReqData) throws Exception {
		// 接受API返回
		String payServiceResponseString;
		
		long costTimeStart = System.currentTimeMillis();

		payServiceResponseString = scanPayQueryService.request(scanPayQueryReqData);
		
		long costTimeEnd = System.currentTimeMillis();
		long totalTimeCost = costTimeEnd - costTimeStart;
		
		log.info("------------------request createPrapareOrder taking ：" + totalTimeCost + "ms"+"--response="+payServiceResponseString);
		
		// 将从API返回的XML数据映射到Java对象
		ScanPayQueryResData scanPayQueryResData = (ScanPayQueryResData) Util.getObjectFromXML(payServiceResponseString,
				ScanPayQueryResData.class);
		
		return scanPayQueryResData;
	}
	
	
	/**
	 * 
	 * @Description:专家提现，商家向用户（该系统中的专家）支付
	 * @author:guchunxia
	 * @param scanPayReqData
	 *            返回类型 void
	 * @throws Exception
	 * @date 2017年5月25日
	 */
	public JsApiPayComResData payComOrder(JsApiPayComService jsApiPayComService ,JsApiPayComReqData jsApiPayComReqData) throws Exception {
		// 接受API返回
		String payServiceResponseString;
		
		long costTimeStart = System.currentTimeMillis();

		payServiceResponseString = jsApiPayComService.request(jsApiPayComReqData);
		
		long costTimeEnd = System.currentTimeMillis();
		
		long totalTimeCost = costTimeEnd - costTimeStart;
		
		log.info("------------------request createPrapareOrder taking ：" + totalTimeCost + "ms"+"--response="+payServiceResponseString);
		
		// 将从API返回的XML数据映射到Java对象
		JsApiPayComResData jsApiPayComResData = (JsApiPayComResData) Util.getObjectFromXML(payServiceResponseString,
				JsApiPayComResData.class);
		
		return jsApiPayComResData;
	}
	
	
	/**
	 * 
	 * @Description:退款，商家向用户（该系统中的专家）支付
	 * @author:guchunxia
	 * @param scanPayReqData
	 *            返回类型 void
	 * @throws Exception
	 * @date 2017年5月25日
	 */
	public RefundResData refundComOrder(RefundService refundService ,RefundReqData refundReqData) throws Exception {
		// 接受API返回
		String payServiceResponseString;
		
		long costTimeStart = System.currentTimeMillis();

		payServiceResponseString = refundService.request(refundReqData);
		
		long costTimeEnd = System.currentTimeMillis();
		
		long totalTimeCost = costTimeEnd - costTimeStart;
		
		log.info("------------------request createPrapareOrder taking ：" + totalTimeCost + "ms"+"--response="+payServiceResponseString);
		
		// 将从API返回的XML数据映射到Java对象
		RefundResData RefundResData = (RefundResData) Util.getObjectFromXML(payServiceResponseString,
				RefundResData.class);
		
		return RefundResData;
	}


}
